<?php
/*
Plugin Name: dKret iCal Event Widget
Description: An additional iCal Calendar Widget for dKret (needs iCal Events Plugin to show up).
Author: J. Kretzschmar
Version: 1.0
Author URI: http://diekretzschmars.de
*/

if (!class_exists('ICalEvents'))
	return;

function widget_dkret_icalevents($args) {
	extract($args);
	$options = get_option('widget_dkret_icalevents');
	$title = empty($options['title']) ? __('Next Events','dkret') : $options['title'];
	if ( ($options['icalurl']) != '' ) {
		echo "\t\t\t$before_widget\n";
		echo "\t\t\t\t$before_title".$title.$after_title;
		echo "\t\t\t\t<ul>";
ICalEvents::display_events('url='. $options['icalurl'] .'&before=<li>&after=</li>&date_format=' .$options['icaldate_format'] .'&time_format=' .$options['icaltime_format'] .'&limit=' .$options['events'] .'&before_date=&after_date=&before_summary=<ul><li>&after_summary=</li></ul>&use_description=0&gmt_start=' . time());
		echo "\t\t\t\t</ul>";
		echo "\n\t\t\t$after_widget";
	}
}

// Admin section
function widget_control_dkret_icalevents() {
 	global $wpdb;
 	$options = get_option('widget_dkret_icalevents');
    		
	if ( $_POST['dkret-submit'] ) {

		$options['title'] = strip_tags(stripslashes($_POST['dkret-icalevent-title']));
		$options['icalurl'] = strip_tags(stripslashes($_POST['dkret-icalevent-url']));		
		$options['icaldate_format'] = strip_tags(stripslashes($_POST['dkret-icalevent-date_format']));		
		$options['icaltime_format'] = strip_tags(stripslashes($_POST['dkret-icalevent-time_format']));		
		$options['events'] = trim($_POST['dkret-icalevents']);
		update_option('widget_dkret_icalevents', $options);
	}
	
	$title = htmlspecialchars($options['title'], ENT_QUOTES);
	$icalurl = $options['icalurl'];
  $icaldateformat = $options['icaldate_format'];
  $icaltimeformat = $options['icaltime_format'];

	$events = $options['events'];

	// The Box content
	echo '<p style="text-align:right;"><label for="dkret-icalevent-title" />' . __('Title:') . ' <input style="width: 200px;" id="dkret-icalevent-title" name="dkret-icalevent-title" type="text" value="'.$title.'" /></p>';
	echo '<p style="text-align:right;"><label for="dkret-icalevent-url" />' . __('iCal URL:', 'dkret') . ' <input style="width: 200px;" id="dkret-icalevent-url" name="dkret-icalevent-url" type="text" value="'.$icalurl.'" /></p>';

	echo '<p style="text-align:right;"><label for="dkret-icalevent-date_format" />' . __('Date format:', 'dkret') . ' <input style="width: 200px;" id="dkret-icalevent-date_format" name="dkret-icalevent-date_format" type="text" value="'.$icaldateformat.'" /></p>';
	echo '<p style="text-align:right;"><label for="dkret-icalevent-time_format" />' . __('Time format:', 'dkret') . ' <input style="width: 200px;" id="dkret-icalevent-time_format" name="dkret-icalevent-time_format" type="text" value="'.$icaltimeformat.'" /></p>';
	echo '<p>' . __('Documentation at: <a href="http://php.net/strftime">php.net/strftime</a>', 'dkret') . '</p>';

  echo '<p style="text-align:right;"><label for="dkret-icalevents" />' . __('Number of Events:', 'dkret') . ' <input size="2" id="dkret-icalevents" name="dkret-icalevents" type="text" value="'.$events.'" /></p>';
  echo '<input type="hidden" id="dkret-submit" name="dkret-submit" value="1" />';
}

$options = get_option('widget_dkret_icalevents');
if ( !is_array($options) )
	$options = array('events'=>'4','icalurl'=>'','icaldate_format'=>'%a, %d.%m.%y','icaltime_format'=>'%H:%M',);
	update_option('widget_dkret_icalevents', $options);

$widget_ops = array('classname' => 'widget_dkret_icalevents', 'description' => __( "Your next iCal events (dkret)", 'dkret') );
wp_register_sidebar_widget('icalevents', __('iCal Events','dkret'), 'widget_dkret_icalevents', $widget_ops);
wp_register_widget_control('icalevents', __('iCal Events','dkret'), 'widget_control_dkret_icalevents');
?>
